package com.hmdp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.*;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.DateReplenishUtil;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private IUserService userService;

    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        blog.setUserId(user.getId());
        // 保存探店博文
        blogService.save(blog);
        // 返回id
        return Result.ok(blog.getId());
    }

    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
       return blogService.likeBlog(id);
    }

    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
       return  blogService.queryHotBlog(current);
    }
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id){
        return blogService.queryBlogById(id);
    }

    @PostMapping("/readExcel")
    public Result readExcel() throws Exception {
        blogService.readExcel();
        return Result.ok("成功");
    }

    @PostMapping("/twoData")
    public Result getTwoData(@RequestBody PublicRequestDTO publicRequestDTO){
        HashMap<String, Object> map = new HashMap<>();
        TwoDataTestResponseDTO twoDataTestResponseData = blogService.getTwoDataTestResponseData(publicRequestDTO);
        map.put("twoDataTestResponseData",twoDataTestResponseData);
        List<PublicDataDTO> twoDataTest = blogService.getTwoDataTest(publicRequestDTO);
        Map<String, PublicDataDTO> collect = twoDataTest.stream().collect(Collectors.toMap(PublicDataDTO::getDate, Function.identity(), (k, v) -> k));
        try {
            twoDataTest = DateReplenishUtil.dateReplenish(collect, new PublicDataDTO(), publicRequestDTO.getStartDate(), publicRequestDTO.getEndDate());
            twoDataTest = twoDataTest.stream().sorted((k, v) -> k.getDate().compareTo(v.getDate())).collect(Collectors.toList());
            map.put("twoDataTest",twoDataTest);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.ok(map);
    }

    // BlogController
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

}
